package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.entity.ApAuthor;
import com.heima.article.mapper.ApAuthorMapper;
import com.heima.article.service.IApAuthorService;
import com.heima.common.dto.ResponseResult;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP文章作者信息表 服务实现类
 * </p>
 *
 * @author wsj
 * @since 2022-03-11
 */
@Service
public class ApAuthorServiceImpl extends ServiceImpl<ApAuthorMapper, ApAuthor> implements IApAuthorService {

    @Override
    public ResponseResult<ApAuthor> saveApAuthor(ApAuthor apAuthor) {
        //根据user_id查询用户是否存在
        LambdaQueryWrapper<ApAuthor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApAuthor::getUserId,apAuthor.getUserId());
        ApAuthor one = getOne(wrapper);
        //存在，则直接返回
        if (one!=null) {
            return ResponseResult.okResult(one);
        }
        //不存在，则新增
        apAuthor.setCreatedTime(new Date());
        save(apAuthor);
        //返回
        return ResponseResult.okResult(apAuthor);
    }
}
